package com.cd.javacxy.common.utils;

import com.alibaba.fastjson.JSON;
import com.cd.javacxy.common.model.vo.AjaxResponse;
import lombok.experimental.UtilityClass;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author yanghy
 * @version V1.0
 * @date 2019/10/30
 */
@UtilityClass
public class ResponseUtil {

    /**
     * 通过response 响应JSON内容
     *
     * @param re       AjaxResponse
     * @param response HttpServletResponse
     * @throws IOException 输出内容时可能发生io异常
     */
    public void writeJavaScript(AjaxResponse re, HttpServletResponse response) throws IOException {
        response.setStatus(200);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(JSON.toJSONString(re));
        printWriter.flush();
    }
}
